package org.easy.socket.server.config;

import org.easy.socket.server.annotation.Socket;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description: 配置类
 * @Author: wxl
 * @CreateTime: 2023/1/11 8:52
 */
@Socket(value = "easy.socket.server")
public class SocketConfig {

    private ServerConfig master;

    private List<ServerConfig> slave;

    public SocketConfig() {
        this.readYaml();
    }

    private void readYaml() {
        Yaml yaml = new Yaml();
        String fileName = "socket.yml";
        InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(fileName);
        Map<String,Map<String,Map<String,Map<String,Object>>>> obj = yaml.load(resourceAsStream);
        String[] values = getAnnotationValue();
        Map<String,Object> objectMap = obj.get(values[0]).get(values[1]).get(values[2]);

        for (Map.Entry<String,Object> entry : objectMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (Objects.nonNull(value)) {
                if (value instanceof String) {
                    String val = (String) value;
                    ServerConfig serverConfig = toServerConfig(val,key);
                    setMaster(serverConfig);
                } else if (value instanceof List) {
                    List<ServerConfig> serverConfigs = ((List<?>) value)
                            .stream()
                            .map(v -> toServerConfig(v.toString(),key))
                            .collect(Collectors.toList());
                    setSlave(serverConfigs);
                }
            }

        }
    }

    private ServerConfig toServerConfig(String value,String key){
        String[] values = value.split("/");
        ServerConfig serverConfig = new ServerConfig();
        serverConfig.setHost(values[0]);
        serverConfig.setPort(Integer.parseInt(values[1]));
        serverConfig.setModel(key);
        if ("slave".equals(key)) {
            if (values.length == 3) {
                if (null == values[2] || values[2].length() == 0) {
                    serverConfig.setWeight(20);
                } else {
                    serverConfig.setWeight(Integer.parseInt(values[2]));
                }

            } else if (values.length == 2){
                serverConfig.setWeight(20);
            }
        }
        return serverConfig;
    }

    private String[] getAnnotationValue () {
        Socket socketAnnotation = getClass().getAnnotation(Socket.class);
        String value = socketAnnotation.value();
        if (Objects.isNull(value) || value.length() == 0) {
            return new String[]{};
        }
        return value.trim().split("\\.");
    }

    public ServerConfig getMaster() {
        return master;
    }

    public void setMaster(ServerConfig master) {
        this.master = master;
    }

    public List<ServerConfig> getSlave() {
        return slave;
    }

    public void setSlave(List<ServerConfig> slave) {
        this.slave = slave;
    }

    @Override
    public String toString() {
        return "SocketConfig{" +
                "master=" + master +
                ", slave=" + slave +
                '}';
    }

}
